/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.View;

import com.lcfgly.Database.ShopOnepage;
import com.lcfgly.Database.UserInfo;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Exception.Message;
import com.lcfgly.Form.Validation.OnepageValidation;
import com.lcfgly.Interface.UserSession;
import com.lcfgly.Service.OnepageService;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author lcfgly
 */
@Controller
@RequestMapping("/control")
public class OnepageControl extends FatherController{
    
    @Autowired
    OnepageService OnepageService;
    
    @RequestMapping(value="/onepage/{id}",method=RequestMethod.GET) 
    public String onepage(HttpServletRequest request,@PathVariable("id") int id)throws ActionException{
        request.setAttribute("info",OnepageService.findById(id));
        return "controller/onepage";  
    }  
    
    @RequestMapping(value = "/onepage/{id}",method = RequestMethod.POST)
    public ModelAndView onepage(@Validated ShopOnepage data,BindingResult res,@PathVariable("id") int id,@UserSession UserInfo session) throws ActionException{
        if(res.hasFieldErrors())
            return new ModelAndView("controller/onepage", "error",getError(res));
        data.setTid(id);
        data.setWriter(session.getUsername());
        data.setWriteruid(session.getUid());
        OnepageService.updateonepage(data, session.getUid());
        throw new Message("修改成功!");
    }
    
    @InitBinder
    public void initValidation(WebDataBinder binder){
        binder.setValidator(new OnepageValidation());
    }
    
}
